/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frc2856.robot;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.StreamConnection;

/**
 *
 * @author Dustin
 */
public class TCPNetworking implements Runnable {

    private Thread thisThread;
    private int serverPort;
    private ServerSocketConnection server;
    private StreamConnection driver;
    private InputStream dataIn;
    private boolean connected = false;
    private boolean stopped = false;
    private String dataPacket = null;
    private String[] dataArray = null;
    private final int DATA_POS = 0, DATA_ANGLE = 1; //These are examples, showing how the data in the array should be laid out
    private long timeStamp = 0;

    public TCPNetworking(int port) {
        serverPort = port;
        try {
            server = (ServerSocketConnection) Connector.open("socket://:" + serverPort);
        } catch (IOException ex) {
            System.err.println(ex);
        }
        thisThread = new Thread(this);
    }

    public void start() {
        stopped = false;
        if (thisThread != null) {
            thisThread.start();
        }
    }

    public void run() {
        double prevValue = 0;
        //SmartDashboard.putNumber("Slider 1", 0.5);
        while (!stopped) {
            
            double curValue = SmartDashboard.getNumber("Slider 1");
            if(curValue!=prevValue)
                System.out.println(curValue);
            prevValue = curValue;
            //System.out.println("in run");
            //this.accept();
            //this.listen();
        }
    }

    public void stop() {
        stopped = true;
        thisThread.interrupt();
    }

    private void accept() {
        try {
            driver = server.acceptAndOpen();
            connected = true;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void listen() {
        try {
            dataIn = driver.openInputStream();
            //while (!stopped) {
            String rcvdStr = "";
            String[] rcvdArray;
            byte[] bytes;
            readloop:while (true) {
                bytes = new byte[20];
                dataIn.read(bytes);
                for (int i = 0; i < bytes.length; i++) {
                    if(bytes[i]==0) {
                        break readloop;
                    }
                    rcvdStr=rcvdStr+(char)bytes[i];
                }
            }
            rcvdArray = Utils.stringSplit(rcvdStr, ';');
            System.out.println(rcvdStr);
            for(int i = 0; i<rcvdArray.length; i++)
                System.out.println("arr "+rcvdArray[i]);
            dataIn.close();
            driver.close();
            

        } catch (EOFException ex) {
            System.out.println("E");
        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("D");
            connected = false;
            driver = null;
            return;

        }


    }

    public String getDataString() {
        return dataPacket;
    }

    public long timeSinceLastPacket() {
        return System.currentTimeMillis() - timeStamp;
    }
}
